
#ifndef _TTG_THREADS_THREADS_
#define _TTG_THREADS_THREADS_
#include "Common/Definitions.h"

#include <map>
#include <vector>
#include "Common/Errors.h"
#include "Common/Math.h"
#include "Common/Structures.h"
#include "CriticalSections.h"

namespace ttg_internal
{
	namespace threads
	{
		//Static class, that provides basic operations for current thread.
		class CurrentThread
		{
			public:
				//Returns ID of current thread.
				static ttg::math::uint64 getIdentifier()
				{ throw ttg::Error(ttg::Error::FATAL, "CurrentThread", "getIdentifier",
								   "Not overrided"); }
				//Suspends current thread.
				//Note: time in seconds.
				static void sleep(double time)
				{ throw ttg::Error(ttg::Error::FATAL, "CurrentThread", "sleep",
								   "Not overrided"); }
		};
		//Base class for any threads.
		class Thread :public ttg::structures::TaggedObject<int>
		{
			protected:
				//Thread suspends itself for long time (via SlowEvent).
				//Can be woken only by wake().
				virtual void sleepDeep() = 0;
				//Threads suspends itself for required time interval (via SlowEvent).
				//Note: it can be rised by "wake()".
				virtual void sleepDeep(double seconds) = 0;
				//Thread suspends itself for short time (via FastEvent).
				//Can be woken only by wake().
				virtual void sleepFast() = 0;
				//Threads suspends itself for required time interval (via FastEvent).
				//Note: it can be rised by "wake()".
				virtual void sleepFast(double seconds) = 0;
				//Enter point. Should never return.
				virtual void work() = 0;
			public:
				//Wakes thread. If it does not sleep, does nothing.
				virtual void wake() = 0;
				//Starts thread (if it wasn't started by constructor).
				virtual void start() = 0;
				//Sends request for delayed termination (which will be perfromed as soon as possible).
				//Blocks current thread.
				virtual void terminate() = 0;
				//Checks thread status.
				virtual bool isSleeping() = 0;
				//Changes type of sleep.
				//If thread does not sleep, does nothing.
				virtual void reSleep(bool fast_sleep) = 0;
				//Tryes to set ideal core.
				virtual void setIdealCore(size_t core) = 0;
				//Returns ID of current thread.
				//Never throws errors.
				static unsigned long long getCurrentThreadID();
				//For descendants.
				virtual ~Thread()
				{ /*nothing*/ }
		};
	}
}

#include "Win32/Win32Threads.h"

#endif
